package mcommand;

import java.awt.Graphics2D;

/**
 * Handles how the enemys missiles are animated on the board.
 * Also handels the drawing of the explosion shape and the
 * target cross for the enemys missiles (only in debug mode).
 *
 * @author Marcel, Christian, Klaus
 */
public class EnemyMissile extends Missile {
    // defines the enemy missile speed
    private static float enemyMissileSpeed = 0.05f;

    // sets the standard target mode for the enemy missiles to NO_TURRETS
    private TargetMode tMode = TargetMode.NO_TURRETS;

    // defines the variables for the boards width and height
    private int board_width;
    private int board_height;
    
    // used in the collision detection, so every missile is only counted once
    private boolean alreadyCounted = false;

    /**
     * Sets how big a explosion should be, the board width and height and
     * creates a random starting position for the enemy missile on the top of
     * the board.
     * 
     * @param board board
     */
    public EnemyMissile(final Board board) {
        // sets the width and height variables
        board_width = board.getWidth();
        board_height = board.getHeight();

        // creates a random x position between 0 and board width
        start_x = (int) ((Math.random()) * board_width);

        // enemy missile starts always on the top of the board
        start_y = 0;

        // sets how big the explosion should be
        explode_size = 20;

        // used for the speed calculation in the Missile class
        std_move_speed = enemyMissileSpeed;
    }

    /**
     * Can change the enemy missiles speed.
     * 
     * @param speed
     */
    public static void setEnemyMissileSpeed(final float speed) {
        enemyMissileSpeed = speed;
    }

    /**
     * Can change the target mode for the missile.
     * 
     * @param tMode tMode
     */
    public final void setTargetMode(final TargetMode tMode) {
        this.tMode = tMode;
    }

    /**
     * Calculate a random target looks which target mode is on for the
     * calculation.
     */
    public final void randomTarget() {
        // initiates the local target position valriables
        int newTarget_x = 0;
        int newTarget_y = 0;

        // if the target mode is EVERYTHING (normally not used-unrealistic)
        if (tMode == TargetMode.EVERYTHING) {

            // sets the target x of the enemy missile somewhere between 0 and
            // board width
            newTarget_x = (int) (Math.random() * board_width);

            // sets the target y on the height of the ground
            newTarget_y = 549;

            // if the target mode is NO_TURRETS
        } else if (tMode == TargetMode.NO_TURRETS) {
            do {

                // calculates a random c between the left an right missile bay
                newTarget_x = (int) (Math.random() * (board_width - 80)) + 40;

                // does the calculation so long till the x value does not hit
                // the middile missile bay
            } while (newTarget_x > 330 && newTarget_x < 470);

            // sets the target y on the height of the ground
            newTarget_y = 549;

            // if the target mode is ONLY_TURRETS
        } else if (tMode == TargetMode.ONLY_TURRETS) {

            // calculates a random number between 0 and 2
            int target_turret = (int) (Math.random() * 3);

            // defines a random number between 1 and 37
            int rand = (int) (Math.random() * 38);

            // if target_turret is 0 then the left missile bay is used
            if (target_turret == 0) {

                // sets the target x somewhere on the left missile bay
                newTarget_x = rand;

                // if target_turret is 1 then the middle missile bay is used
            } else if (target_turret == 1) {

                // sets the target x somewhere on the middle missile bay
                newTarget_x = rand + 382;

                // if target_turret is 2 then the right missile bay is used
            } else {

                // sets the target x somewhere on the right missile bay
                newTarget_x = rand + 764;
            }
            // sets the y for the missile to the height of the missile bays
            newTarget_y = 522;
        }
        // calls now the setTarget method to set the calculated positions for
        // the missile on the board
        setTarget(newTarget_x, newTarget_y);
    }

    /**
     * Draws the actual enemy missile position.
     * If the missile state is DESTROYED then a explosion shape
     * will be painted if debug mode is on the
     * target for the enemy missile will be shown with a cross
     * 
     * @param g2d g2d
     */
    public final void paint(final Graphics2D g2d) {
        // if the missile state is FLYING
        if (ms == MissileState.FLYING) {

            // draws a line with the start positions and the current end
            // positions
            g2d.drawLine(start_x, start_y, getX(), getY());

            // draws the target cross on the target position of the missile if
            // debug mode is on
            if (Board.DEBUG) {
                g2d.drawImage(targetCross,
                        (target_x - (targetCross.getWidth(null) / 2)),
                        (target_y - (targetCross.getHeight(null) / 2)), null);
            }

            // if missile state is DESTROYED
        } else if (ms != MissileState.DESTROYED) {

            // calls the method of missile to get the explosion shape to draw
            g2d.draw(getExplosionShape());
        }
    }

    /**
     * Sets the missile is already counted (true or false).
     * @param value value
     */
    public final void setCounted(final boolean value) {
        alreadyCounted = value;
    }

    /**
     * Returns if the missile is already counted (true or false).
     * @return alreadyCounted
     */
    public final boolean isAlreadyCounted() {
        return alreadyCounted;
    }

    /**
     * Holds the possible target modes (EVERYTHING, NO_TURRETS, ONLY_TURRETS).
     * 
     */
    public enum TargetMode {
        EVERYTHING,
        NO_TURRETS,
        ONLY_TURRETS
    }
}
